package jm.mq.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import jm.mq.utils.RabbitMQUtils;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @Description direct 路由模式
 * @date 2022/3/8 9:01
 */
public class Producer {
    private static final String EXCHANGE_NAME = "direct_mq";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();

        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        Scanner scanner = new Scanner(System.in);

        int i = 1;
        while (scanner.hasNext()){
            i++;
            String msg = scanner.next();
            if ( i % 2 == 0){
                channel.basicPublish(EXCHANGE_NAME,"info",null,msg.getBytes(StandardCharsets.UTF_8));
            }else {
                channel.basicPublish(EXCHANGE_NAME,"error",null,msg.getBytes(StandardCharsets.UTF_8));
            }
            System.out.println("发送消息：" + msg);
        }
    }
}
